package com.movie.modules.jf.service.impl;

import com.movie.common.utils.NumberUtils;
import com.movie.common.utils.R;
import com.movie.modules.jf.dao.RelationDao;
import com.movie.modules.jf.entity.RelationEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.common.utils.PageUtils;
import com.movie.common.utils.Query;

import com.movie.modules.jf.dao.TypeDao;
import com.movie.modules.jf.entity.TypeEntity;
import com.movie.modules.jf.service.TypeService;
import org.springframework.transaction.annotation.Transactional;


@Service("typeService")
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements TypeService {

    @Autowired
    private TypeDao typeDao;

    @Autowired
    private RelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String typeName = params.get("typeName")==null?null:params.get("typeName").toString();
        Integer type= NumberUtils.objToInteger(params.get("type"));
        IPage<TypeEntity> page = this.page(
                new Query<TypeEntity>().getPage(params),
                new QueryWrapper<TypeEntity>().like(StringUtils.isNotEmpty(typeName),"type_name",typeName).eq(type!=null,"type",type).orderByDesc("type_id")
        );

        return new PageUtils(page);
    }

    @Override
    public List<TypeEntity> getByMovieId(Integer id) {
        return typeDao.getByMovieId(id);
    }

    @Override
    public List<TypeEntity> getByNewsId(Integer id) {
        return typeDao.getByNewsId(id);
    }

    @Override
    public Map<String, List<TypeEntity>> getSelect(Integer type) {
        Map<String,List<TypeEntity>> map=new HashMap<>();
        map.put("list",this.list(new QueryWrapper<TypeEntity>().eq("status",1).eq("type",type)));
        return map;
    }

    @Override
    public R delete(Integer typeId,Integer type) {
        List<RelationEntity> list=relationDao.selectList(new QueryWrapper<RelationEntity>().eq("relation_type",type).eq("relation_id",typeId));
        if(list.size()>0){
            return R.error("该类型已被使用，不能删除");
        }
        this.removeById(typeId);
        return R.ok("删除成功");
    }

}